﻿using Microsoft.EntityFrameworkCore;
using SheepStorage.Domain.DTO.InputDto;
using SheepStorage.Domain.DTO.Output;
using SheepStorage.Domain.Entity;
using SheepStorage.Repository.Interface;
using SheepStorage.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SheepStorage.Service.DataImp
{
    /// <summary>
    /// 企业服务实现
    /// </summary>
    public class DepartmentService : IDepartmentService
    {

        private readonly IBaseRepository<Department> repository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="repository"></param>
        public DepartmentService(IBaseRepository<Department> repository)
        {
            this.repository = repository;   
        }

        /// <summary>
        /// 部门添加
        /// </summary>
        /// <param name="department"></param>
        /// <returns></returns>
        public async Task<int> AddDepartment(Department department)
        {
            return repository.Add(department);
        }

        public int AddDepartments(Department department)
        {
            return repository.Add(department);
        }
        /// <summary>
        /// 部门删除
        /// </summary>
        /// <param name="departmentId"></param>
        /// <returns></returns>
        public int DelDepartment(int departmentId)
        {
            return repository.Del(departmentId);
        }
       /// <summary>
       /// 部门显示
       /// </summary>
       /// <param name="departmentParentId"></param>
       /// <returns></returns>

        public async Task<List<Cascader>> GetCascader(int departmentParentId)
        {
            var list=await repository.GetAll().
                Where(c=>c.DepartmentParentId==departmentParentId).ToListAsync();   
            var entity=new List<Cascader>();
            foreach (var item in list)
            {
                var listItem = new Cascader()
                {
                    Value = item.DepartmentId,
                    Label = item.DepartmentName
                };
                listItem.Children = await GetCascader(item.DepartmentId);
                entity.Add(listItem);
            };
            return entity;
        }

        /// <summary>
        /// 显示--导出
        /// </summary>
        /// <returns></returns>
        public async Task<List<Department>> GetDepartment()
        {
            return repository.GetAll().ToList();
           
             
        }

        /// <summary>
        /// 部门显示
        /// </summary>
        /// <returns></returns>
        public async Task<PageOutput<Department>> GetDepartmentList(string? DepartmentName, int pageIndex, int pageSize)
        {
            var list = repository.GetAll().ToList();
            if (!string.IsNullOrEmpty(DepartmentName))
            {
                list = list.Where(c => c.DepartmentName.Contains(DepartmentName)).ToList();
            }
            var result = new PageOutput<Department>()
            {
                Total = list.Count(),
                PageSize = pageSize,
            };
            var entity = list.OrderByDescending(c => c.DepartmentId).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            result.Data = entity;
            return result;
           
        }

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="departmentId"></param>
        /// <returns></returns>
        public Department GetFtDepartment(int departmentId)
        {
            return repository.GetValue(departmentId);
        }

        /// <summary>
        /// 部门修改
        /// </summary>
        /// <param name="department"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> UpdaDepartment(Department department)
        {
           return repository.Update(department);
        }







    }
}
